<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd" style="margin-right: 5px">新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableActions(record)"/>
      </template>
    </BasicTable>
    <!-- <a-modal v-model:open="isShowCopyModal" title="请输入复制后的模板名称" @ok="handlerDoCopy">
      <a-input v-model:value="copyGroupName" placeholder="名称" />
    </a-modal> -->
    <TemplateCopyModal @register="registerCopyModal" @success="handleTemplateCopySuccess"/>
    <TemplateModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="gen-code-template" setup>
import {onMounted, ref, unref} from 'vue';
import {BasicTable, TableAction} from '/@/components/Table';
import {useModal} from '/@/components/Modal';
import TemplateCopyModal from './TemplateCopyModal.vue';
import {
  batchDeleteTemplate,
  deleteTemplate,
  getExportUrl,
  getImportUrl,
  pageTemplate
} from './template.api';
import {listTemplateGroup} from '../templateGroup/templateGroup.api';
import {columns, searchFormSchema} from './template.data';
import TemplateModal from './TemplateModal.vue';
import {useMessage} from '/@/hooks/web/useMessage';
import {useListPage} from '/@/hooks/system/useListPage';
import {Icon} from '/@/components/Icon'

const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerCopyModal, { openModal: openCopyModal }] = useModal();

  const currentGroupId = ref('')
  // 列表页面公共参数、方法
  const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
    designScope: 'gen-code-template',
    tableProps: {
      title: '代码模板列表',
      api: pageTemplate,
      columns: columns,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
      },
      bordered: false,
      actionColumn: {
        width: 140,
        title: '操作',
        dataIndex: 'action',
        fixed: 'right',
        slots: { customRender: 'action' },
      },
    },

    exportConfig: {
      name:"模板列表",
      url: getExportUrl
    },
    importConfig: {
      url: () => {
        createMessage.loading({
          content: `正在导入模板...`,
          key: 'template_import_config',
          duration: 0,
        });
        return `${getImportUrl}?groupId=${currentGroupId.value}`
      },
      fail: () => {
        createMessage.destroy('template_import_config');
      },
      success: () => {
        createMessage.destroy('template_import_config');
        reload()
      }
    }
  });

  const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext;

  //在组件被挂载之后调用(vue3)
  onMounted(async () => {
    //将数据放入下拉框中
    const { updateSchema } = getForm();
    const option = await listTemplateGroup({});
    await updateSchema({
      field: 'groupId',
      componentProps: {
        optionFilterProp: "label",
        showSearch: true,
        options: unref(option),
        onSelect: (value: any, option: any) => {
          console.log("option", option);
          currentGroupId.value = option.value
          reload()
        },
      },
    });
  });

  /**
   * 操作列定义
   * @param record
   */
  function getTableActions(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '复制',
        popConfirm: {
          title: '是否确认复制模板',
          confirm: handleCopy.bind(null, record),
        }
      },
      {
        label: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
      
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑事件
   */
  function handleCopy(record) {
    openCopyModal(true, record)
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  function handleTemplateCopySuccess() {
    reload()
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteTemplate({ id: record.id }, reload);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteTemplate({ ids: selectedRowKeys.value }, reload);
  }
</script>
